package cn.touna.jss.modules.custom.batch;


import com.alibaba.fastjson.JSONObject;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.StringWriter;

public class FahaiPersonRequestProcessor implements ItemProcessor {

    private String url;
    private RestTemplate restTemplate = null;
    private HttpHeaders headers;
    private Template template;

    public FahaiPersonRequestProcessor(String url) throws IOException {
        this.url = url;
        headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        String formatStr = "{\"bizType\":\"\",\"method\":\"fahai.personPreciseQuery\",\"version\":\"1.0\",\"params\":{\"name\":\"${姓名}\",\"idCard\":\"${身份证号}\",\"datatype\":\"${datatype}\"}}";
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        StringTemplateLoader loader = new StringTemplateLoader();

        String DEFAULT = "default";
        loader.putTemplate(DEFAULT,formatStr);
        cfg.setTemplateLoader(loader);
        template = cfg.getTemplate(DEFAULT);
    }

    @Override
    public JSONObject process(JSONObject item) {
        restTemplate = new RestTemplate();
//        类别名称	英文代码
//        裁判文书	cpws
//        执行公告	zxgg
//        失信公告	shixin
//        开庭公告	ktgg
//        法院公告	fygg
//        逾期催收名单	wdhmd
//        案件流程信息	ajlc
//        曝光台	bgt
        //姓名  身份证号   案件流程  曝光台  裁判文书  法院公告  开庭公告  失信执行人  执行公告  司法总数  总数
        String datatype = "";
        JSONObject result = new JSONObject();
        //案件流程
        int ajlcCount = getCount(result,item,"ajlc","案件流程");
        int bgtCount = getCount(result,item,"bgt","曝光台");
        int cpwsCount = getCount(result,item,"cpws","裁判文书");
        int fyggCount = getCount(result,item,"fygg","法院公告");
        int ktggCount = getCount(result,item,"ktgg","开庭公告");
        int shixinCount = getCount(result,item,"shixin","失信执行人");
        int zxggCount = getCount(result,item,"zxgg","执行公告");
        int wdhmdCount = getCount(result,item,"wdhmd","逾期催收名单");
        int sum = ajlcCount + bgtCount + cpwsCount + fyggCount + ktggCount + shixinCount + zxggCount + wdhmdCount;
        result.put("司法总数",sum);
        return result;
    }

    private int getCount(JSONObject result,JSONObject item,String datatype,String header){
        JSONObject reqObj = (JSONObject) item.clone();
        reqObj.put("datatype",datatype);
        JSONObject resObj = postForData(reqObj);

        String code = resObj.getString("code");
        result.put(datatype,resObj);
        int count = 0;
        if("200".equals(code)){
            JSONObject data = JSONObject.parseObject(resObj.getString("data"));
            count = getField(data,datatype+"Count");
        }
        result.put(header,count);
        return count;
    }

    private int getField(JSONObject obj,String field){
        if(obj == null)
            return 0;

        if(obj.containsKey("count")){
            int count = obj.getInteger("count");
            return count;
        }
        if(obj.containsKey(field)){
            String value = obj.getString(field);
            if(StringUtils.isEmpty(value))
                return 0;
            try{
                return Integer.parseInt(value);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return 0;
    }

    private JSONObject postForData(JSONObject req){
        try{
            StringWriter stringWriter = new StringWriter();
            template.process(req,stringWriter);
            String request = stringWriter.toString();
            HttpEntity<String> requestEntity = new HttpEntity<>(request, headers);
            String res = restTemplate.postForObject(url,requestEntity , String.class );
            return JSONObject.parseObject(res);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
